package cn.chiship.bs.api.biz.controller;


import cn.chiship.bs.api.biz.entity.OrderHeader;
import cn.chiship.bs.api.biz.entity.OrderHeaderExample;
import cn.chiship.bs.api.biz.pojo.dto.OrderCreateDto;
import cn.chiship.bs.api.biz.service.BaseService;
import cn.chiship.bs.api.biz.service.OrderHeaderService;
import cn.chiship.bs.api.biz.service.UserCacheService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单控制层
 * 2023/3/26
 *
 * @author lijian
 */
@RestController
@RequestMapping("/orderHeader")
@Api(tags = "订单")
public class OrderHeaderController extends BaseController<OrderHeader, OrderHeaderExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderHeaderController.class);

    @Resource
    private OrderHeaderService orderHeaderService;

    @Resource
    UserCacheService userCacheService;

    @Override
    public BaseService getService() {
        return orderHeaderService;
    }

    @ApiOperation(value = "订单分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", dataType = "Byte", paramType = "query"),

    })
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(
            @RequestParam(required = false, defaultValue = "", value = "keyword") String keyword,
            @RequestParam(required = false, defaultValue = "", value = "orderStatus") Byte orderStatus) {
        OrderHeaderExample orderHeaderExample = new OrderHeaderExample();
        //创造条件
        OrderHeaderExample.Criteria orderHeaderExampleCriteria = orderHeaderExample.createCriteria();
        orderHeaderExampleCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(keyword)) {
            orderHeaderExampleCriteria.andOrderNameLike(keyword + "%");
        }
        if (!StringUtil.isNull(orderStatus)) {
            orderHeaderExampleCriteria.andOrderStatusEqualTo(orderStatus);
        }
        return super.responseEntity(BaseResult.ok(super.page(orderHeaderExample)));
    }


    @ApiOperation(value = "订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", dataType = "Byte", paramType = "query"),

    })
    @GetMapping(value = "/list")
    public ResponseEntity<BaseResult> list(
            @RequestParam(required = false, defaultValue = "", value = "userId") String userId,
            @RequestParam(required = false, defaultValue = "", value = "keyword") String keyword,
            @RequestParam(required = false, defaultValue = "", value = "orderStatus") Byte orderStatus) {
        OrderHeaderExample orderHeaderExample = new OrderHeaderExample();
        //创造条件
        OrderHeaderExample.Criteria orderHeaderExampleCriteria = orderHeaderExample.createCriteria();
        orderHeaderExampleCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(keyword)) {
            orderHeaderExampleCriteria.andOrderNameLike(keyword + "%");
        }
        if (!StringUtil.isNull(orderStatus)) {
            orderHeaderExampleCriteria.andOrderStatusEqualTo(orderStatus);
        }
        if (!StringUtil.isNull(userId)) {
            orderHeaderExampleCriteria.andUserIdEqualTo(userId);
        }
        orderHeaderExample.setOrderByClause("gmt_created desc");
        return super.responseEntity(BaseResult.ok(super.list(orderHeaderExample)));
    }


    @ApiOperation(value = "删除订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键ID集合", required = true, dataType = "List", paramType = "body")
    })
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody List<String> ids) {
        OrderHeaderExample orderHeaderExample = new OrderHeaderExample();
        orderHeaderExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(orderHeaderExample));
    }

    @ApiOperation(value = "创建订单")
    @ApiImplicitParams({
    })
    @PostMapping(value = "/createOrder")
    public ResponseEntity<BaseResult> createOrder(
            @RequestBody OrderCreateDto orderCreateDto) {
        return new ResponseEntity(orderHeaderService.createOrder(orderCreateDto), HttpStatus.OK);
    }

    @ApiOperation(value = "订单支付")
    @ApiImplicitParams({
    })
    @PostMapping(value = "/orderPay")
    public ResponseEntity<BaseResult> orderPay(
            @RequestBody String orderId) {
        return new ResponseEntity(orderHeaderService.changeStatus(orderId, Byte.valueOf("2")), HttpStatus.OK);
    }

    @ApiOperation(value = "订单配送")
    @ApiImplicitParams({
    })
    @PostMapping(value = "/orderSend")
    public ResponseEntity<BaseResult> orderSend(
            @RequestBody String orderId) {
        return new ResponseEntity(orderHeaderService.changeStatus(orderId, Byte.valueOf("3")), HttpStatus.OK);
    }

    @ApiOperation(value = "确认收货")
    @ApiImplicitParams({
    })
    @PostMapping(value = "/orderFinish")
    public ResponseEntity<BaseResult> orderFinish(
            @RequestBody String orderId) {
        return new ResponseEntity(orderHeaderService.changeStatus(orderId, Byte.valueOf("4")), HttpStatus.OK);
    }

    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
    })
    @PostMapping(value = "/orderCancel")
    public ResponseEntity<BaseResult> orderCancel(
            @RequestBody String orderId) {
        return new ResponseEntity(orderHeaderService.changeStatus(orderId, Byte.valueOf("1")), HttpStatus.OK);
    }
}